package com.launch.wlfw.controller;

import com.github.pagehelper.PageInfo;
import com.launch.wlfw.common.Result;
import com.launch.wlfw.pojo.ReaderInfo;
import com.launch.wlfw.service.ReaderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 读者信息控制器
 *
 * @author liuwei
 * @ClassName ReaderInfoController.java
 * @description: TODO
 * @date 2023年06月30日
 * @version: 1.0
 */
@Slf4j
@Controller
public class ReaderInfoController {

    /**
     * 读者信息服务
     */
    @Resource
    private ReaderInfoService readerInfoService;


//    /**
//     * 分页查询阅读列表
//     */
//    @GetMapping("/readerAll")
//    @ResponseBody
//    public DataInfo page(ReaderInfo readerInfo, @RequestParam(defaultValue = "1") Integer pageNum,
//                         @RequestParam(defaultValue = "15") Integer pageSize){
//        System.out.println("进入查询 阅读列表接口，参数：");
//        PageInfo<ReaderInfo> readerList = readerInfoService.findReaderList(readerInfo, pageNum, pageSize);
//        return DataInfo.ok("成功 ", readerList);
//    }

    /**
     * 页面
     * 分页查询阅读列表
     *
     * @param readerInfo 读者信息
     * @param pageNum    页面num
     * @param pageSize   页面大小
     * @return {@link Result}<{@link PageInfo}<{@link ReaderInfo}>>
     */

    @GetMapping("/readerAll")
    @ResponseBody
    public Result<PageInfo<ReaderInfo>> page(ReaderInfo readerInfo, @RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize){
        return Result.success(readerInfoService.findReaderList(readerInfo, pageNum, pageSize));
    }

    /**
     * 读者指数
     *
     * @return {@link String}
     */
    @RequestMapping("/readerIndex")
    public String readerIndex() {
        return "reader/readerIndex";
    }

    /**
     * 添加新的阅读者
     * @return {@link String}
     */
    @RequestMapping("/addReaderSubmit")
    @ResponseBody
    public Result<ReaderInfo> add(@RequestBody ReaderInfo readerInfo){
        readerInfoService.insertReaderList(readerInfo);
        return Result.success(readerInfo);
    }

    @RequestMapping("/readerAdd")
    public String selectAddRrader(){
        return "reader/readerAdd";
    }


    /**
     * 删除
     *
     * @param ids id
     * @return {@link Result}<{@link ReaderInfo}>
     *///由于多写@PathVariable导致删除出错
    @RequestMapping("/deleteReader")
    @ResponseBody
    public Result<ReaderInfo>  delete(String ids){
        List<String> list = Arrays.asList(ids.split(","));
        readerInfoService.deleteReaderList(list);
        return Result.success();
    }


    /**
     * 查询读者信息通过id
     *
     * @param id    id
     * @param model 模型
     * @return {@link String}
     */
    @RequestMapping("/queryReaderInfoById")
    public String queryReaderInfoById(Integer id, Model model){
        model.addAttribute("readerInfo", readerInfoService.findReaderById(id));
        return "reader/updateReader";
    }


    /**
     * 更新读者
     *
     * @param readerInfo 读者信息
     * @return {@link Result}<{@link ReaderInfo}>
     */
    @RequestMapping("/updateReaderSubmit")
    @ResponseBody
    public Result<ReaderInfo> updateReader(@RequestBody ReaderInfo readerInfo) {
        readerInfoService.updateReaderList(readerInfo);
        return Result.success(readerInfo);
    }
}
